<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if (UV::readMQ($route->POST("act")) == "post")
{
	$sender_id = $user->getUserId();
	$recipient_id = UV::readInt($route->POST("whom"));
	$header = UV::readMQ(trim($route->POST("header")));
	if ($header == "")
		$header = "Без темы";
	$text = UV::readMQ(trim($route->POST("text")));
	if ($text == "")
		$text = "Без текста";
	$time = time();
	
	$success = $db->sendQuery("insert into `messages` values (NULL,'$sender_id','$recipient_id','$header','$text','$time',0,1,1)");
	
	if ($success)
		goToPage("/admin/messages/sended");
}

require_once ADMIN.DS."head.php";
?>

<div class="full">
	<div class="submenu_container">
		<a class="submenu_button" href='/admin/messages/received'>Полученные</a>
		<a class="submenu_button" href='/admin/messages/sended'>Отправленные</a>
		<a class="submenu_button submenu_button_active" href='/admin/messages/send'>Отправить</a>
	</div>
	<h3>Отправить сообщение</h3>
	<?php
	if (isset($success) && !success)
		echo "<p style='color:red'>Ошибка при отправке письма</p>";
	?>
	<form method='post'>
		<input type='hidden' name='act' value='post' />
		<table cellspacing='10'>
			<tr>
				<td>Кому:</td>
				<td>
					<select name='whom' style="width: 300px"><?php
						$user_list = $db->getTable("select `user_id`,`name`,`sname`,`login` from `users` where `user_id` != '".$user->getUserId()."' order by `name`");
						foreach($user_list as $whom_user)
						{
							echo "<option value='".$whom_user['user_id']."'";
							if ($route->getAdminId() == $whom_user['user_id'])
								echo " selected='selected'";
							echo ">".$whom_user['name']." ".$whom_user['sname']." (".$whom_user['login'].")</option>";
						}
					?></select>
				</td>
			</tr>
			<tr valign='top'>
				<td>Тема:</td>
				<td><input type='text' style="width:300px" name='header' /></td>
			</tr>
			<tr valign='top'>
				<td>Текст:</td>
				<td><textarea rows='5' style="width:300px" name='text'></textarea></td>
			</tr>
			<tr>
				<td colspan='2' align='right'><input type='submit' value='Отправить' /></td>
			</tr>
		</table>
	</form>
</div>

<?php require_once ADMIN.DS."footer.php"; ?>
